

function doAjaxPost() {
	var $form = $('#newprojectform');
	
	$form.find('.control-group').removeClass('error');
	$form.find('.help-inline').empty();
	$form.find('.alert').removeClass('alert-error');
	
	alert('Before submit');
	
	$.ajax({
		type : "POST",
		url : "/req1/saveproject.json",
		data : $("#newprojectform").serialize(),
		success : onSuccess
	});
}

$(document).ready(function(e) {
	
	alert('Binding to submit');
	
	$('#newprojectform').submit(function() {
		doAjaxPost();
		alert('request send');
		return false;
	});

}); // end document ready

function onSuccess(response) {
	
	alert('Success');
	
	if (response.status == 'SUCCESS') {
		
		
		
		$('#message').addClass('alert alert-success');
		$('#message')
				.html('<button type="button" class="close" data-dismiss="alert">&times;</button>' + response.message);

		$('#message').show('slow');

		$('#newprojectform input').attr('readonly', 'readonly');
		$('#newprojectform textarea').attr('readonly', 'readonly');
		$('#newprojectform select').prop('disabled', 'disabled');
		
		$('#newprojectform button').addClass('disabled');
		
		$('#newprojectform a').removeClass('disabled');
		
	}
	
	else if (response.status == 'FAIL') {
		
		for(i = 0 ; i < response.result.length ; i++){
			
			var item = response.result[i];
			var $controlGroup = $('#cg' + item.fieldName);
			$controlGroup.addClass('error');
			
			$controlGroup.find('.help-inline').html(item.message);
		}
		
		
	}
	else if (response.status == 'ERROR'){
		
		
		$('#message').addClass('alert alert-error');
		$('#message')
				.html('<button type="button" class="close" data-dismiss="alert">&times;</button>' + response.message);

		$('#message').show('slow');
	}

}

